unit untDTSFmt;

interface

uses
  Classes, SysUtils, IniFiles, CheckLst, StrUtils;

const
  INI_DTS = 'DTS';

type
  TDTSFmt = class
  private
    FDriverType: integer;
    FConnectionString: string;
    FTableName:  string;
    FProtocol:   string;
    FPort:       integer;
    FDatabase:   string;
    FHostName:   string;
    FCatelog:    string;
    FPassword:   string;
    FUserName:   string;
    FProperties: string;
    FDestFields: string;
    FDTSSQL:     string;
  protected

    function StringDelCRLF(AStrLst: string): string;

    function PropToStr(AStr: string): string;
    function StrToProp(AStr: string): string;

  public
    procedure LoadFromFile(AFileName:string);
    procedure SaveToFile(AFileName: string);
    function FieldToStr(ACheck: TCheckListBox): string;
    procedure StrToField(ACheck: TCheckListBox; AStr: string);
  public
    property DriverType: integer Read FDriverType Write FDriverType;
    property ConnectionString: string Read FConnectionString Write FConnectionString;
    property TableName: string Read FTableName Write FTableName;
    property Protocol: string Read FProtocol Write FProtocol;
    property Database: string Read FDatabase Write FDatabase;
    property HostName: string Read FHostName Write FHostName;
    property Port: integer Read FPort Write FPort;
    property Catelog: string Read FCatelog Write FCatelog;
    property UserName: string Read FUserName Write FUserName;
    property Password: string Read FPassword Write FPassword;
    property Properties: string Read FProperties Write FProperties;
    property DestFields: string Read FDestFields Write FDestFields;
    property DTSSQL: string Read FDTSSQL Write FDTSSQL;
  end;

implementation

{ TDTSFmt }

function TDTSFmt.FieldToStr(ACheck: TCheckListBox): string;
var
  i: Integer;
  s: string;
begin
  s:= EmptyStr;
  for i := 0 to ACheck.Items.Count - 1 do
  begin
    if ACheck.Checked[i] then
      s := s + ACheck.Items[i]+'+\n'
    else
      s := s + ACheck.Items[i]+'-\n'
  end;
  Result := s;  
  FDestFields := Result;
end;

procedure TDTSFmt.LoadFromFile(AFileName: string);
begin
  //
  with TIniFile.Create(AFileName) do
  begin
    FDriverType:=ReadInteger(INI_DTS, 'DriverType', 0);
    FConnectionString:=ReadString(INI_DTS,'ConnString',EmptyStr);
    FTableName:=ReadString(INI_DTS, 'TableName', EmptyStr);
    FProtocol:=ReadString(INI_DTS, 'Protocol', EmptyStr);
    FPort:=ReadInteger(INI_DTS, 'Port', 0);
    FDatabase:=ReadString(INI_DTS, 'Database', EmptyStr);
    FHostName:=ReadString(INI_DTS, 'HostName', EmptyStr);
    FCatelog:=ReadString(INI_DTS, 'Catelog', EmptyStr);
    FPassword:=ReadString(INI_DTS, 'Password', EmptyStr);
    FUserName:=ReadString(INI_DTS, 'UserName', EmptyStr);
    FProperties:=StrToProp(ReadString(INI_DTS, 'Properties', EmptyStr));

    FDestFields:=ReadString(INI_DTS, 'DestFields', EmptyStr);

    FDTSSQL:=ReadString(INI_DTS, 'DTSSQL', EmptyStr);
    Free;
  end;
end;

function TDTSFmt.PropToStr(AStr: string): string;
begin
  Result := StringReplace(AStr, #13#10, '\n', [rfReplaceAll, rfIgnoreCase]);
end;

procedure TDTSFmt.SaveToFile(AFileName: string);
begin
  //
  with TIniFile.Create(AFileName) do
  begin
    WriteInteger(INI_DTS, 'DriverType', FDriverType);
    WriteString(INI_DTS,'ConnString',FConnectionString);
    WriteString(INI_DTS, 'TableName', FTableName);
    WriteString(INI_DTS, 'Protocol', FProtocol);
    WriteInteger(INI_DTS, 'Port', FPort);
    WriteString(INI_DTS, 'Database', FDatabase);
    WriteString(INI_DTS, 'HostName', FHostName);
    WriteString(INI_DTS, 'Catelog', FCatelog);
    WriteString(INI_DTS, 'Password', FPassword);
    WriteString(INI_DTS, 'UserName', FUserName);
    WriteString(INI_DTS, 'Properties', PropToStr(FProperties));

    WriteString(INI_DTS, 'DestFields', FDestFields);
    
    WriteString(INI_DTS, 'DTSSQL', StringDelCRLF(FDTSSQL));
    Free;
  end; 
end;

function TDTSFmt.StringDelCRLF(AStrLst: string): string;
begin
  Result := StringReplace(AStrLst, #13#10,' ',[rfReplaceAll, rfIgnoreCase]);
end;

procedure TDTSFmt.StrToField(ACheck: TCheckListBox; AStr: string);
var
  i: Integer;
  s: string;
begin
  ACheck.Items.Clear;
  ACheck.Items.Text := StringReplace(AStr, '\n', #13#10, [rfReplaceAll, rfIgnoreCase]);
  for i := 0 to ACheck.Items.Count - 1 do
  begin
    s := ACheck.Items[i];
    ACheck.Checked[i] := s[Length(s)] = '+';
    ACheck.Items[i] := LeftStr(s, Length(s)-1);
  end;  
end;

function TDTSFmt.StrToProp(AStr: string): string;
begin
  Result := StringReplace(AStr, '\n', #13#10, [rfReplaceAll, rfIgnoreCase]);
end;

end.
